package cn.miao.photoprinter.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.miao.photoprinter.R;
import cn.miao.photoprinter.entity.MenuItem;

public class MenuAdapter extends BaseAdapter {

    private LayoutInflater infalter;
    private ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();

    public MenuAdapter(Context context) {
        infalter = LayoutInflater.from(context);
    }
    
    public MenuAdapter(Context context, ArrayList<MenuItem> menuItems) {
        infalter = LayoutInflater.from(context);
        this.menuItems = menuItems;
    }

    @Override
    public int getCount() {
        return menuItems.size();
    }

    @Override
    public MenuItem getItem(int position) {
        return menuItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addAll(ArrayList<MenuItem> paths) {
        this.menuItems.clear();
        this.menuItems.addAll(paths);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (null == convertView) {
            convertView = infalter.inflate(R.layout.item_menu, parent, false);
            holder = new ViewHolder();
            holder.tvMenu = (TextView) convertView.findViewById(R.id.tv_menu);
            holder.tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvMenu.setText(menuItems.get(position).getText());
        if (menuItems.get(position).getNumber() > 0) {
        	holder.tvNum.setText("" + menuItems.get(position).getNumber());
        	holder.tvNum.setVisibility(View.VISIBLE);
        } else {
        	holder.tvNum.setVisibility(View.GONE);
        }
        return convertView;
    }
    
    
    class ViewHolder {
    	TextView tvMenu;
        TextView tvNum;
    }

}
